package com.framework.foundation.weixin.wxmac;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;

import com.framework.foundation.common.BaseService;
import com.framework.foundation.common.vo.BTResult;
import com.framework.foundation.common.vo.IResult;
import com.framework.foundation.common.vo.Result;
import com.framework.foundation.util.ApiConfigUtils;
import com.framework.foundation.weixin.api.WifiApi;
import com.framework.foundation.weixin.wxdevice.Wxdevice;
import com.framework.foundation.weixin.wxstore.Wxstore;
import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.weixin.sdk.api.ApiResult;

public class WxmacService extends BaseService<Wxmac> {

	public static final Log LOG = Log.getLog(WxmacService.class);

	public static final WxmacService me = Enhancer.enhance(WxmacService.class);

	/**
	 * 新增方法
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2015年11月10日 下午1:58:08
	 */
	@Override
	@Before(Tx.class)
	public IResult<Object> save(Wxmac model) {
		try {
			if (null == model) {
				return Result.createFail(model);
			}
			return Result.create(model.save(), model);
		} catch (Exception e) {
			throw new RuntimeException("新增异常！", e);
		}
	}

	/**
	 * 编辑方法
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2015年11月10日 下午1:58:08
	 */
	@Override
	public IResult<Object> update(Wxmac model) {
		try {
			if (null == model) {
				return Result.createFail(model);
			}
			return Result.create(model.update(), model);
		} catch (Exception e) {
			throw new RuntimeException("编辑异常！", e);
		}
	}

	@Override
	public BTResult<Record> paginate(Map<String, Object> param) {
		String select = "SELECT *";
		String from = "FROM ww_mac WHERE device_id = ? ORDER BY id ASC";

		Page<Record> pages = Db.paginate(MapUtils.getInteger(param, "pageNumber"),
				MapUtils.getInteger(param, "pageSize"), select, from, param.get("device_id"));
		return new BTResult<Record>(pages.getTotalRow(), pages.getList());
	}

	/**
	 * 逻辑删除模型
	 * 
	 * @param id数组
	 * @return
	 * @Author: Brin
	 * @Date: 2015年11月10日 下午1:58:08
	 */
	@Override
	public IResult<Object> delete(String[] ids) {
		try {
			if (ArrayUtils.isEmpty(ids)) {
				return Result.createFail(0);
			}
			// 微信删除
			for (String id : ids) {
				Wxmac mac = Wxmac.me.findById(Integer.parseInt(id));
				ApiConfigUtils.setApiConfig(mac.getInt("wxuser_id"));
				ApiResult result = WifiApi.deleteMac(mac);
				if (result.isSucceed()) {
					mac.delete();
				} else {
					return Result.createFail(result.getStr("errmsg"));
				}
			}
			return Result.createSuccess();

		} catch (Exception e) {
			throw new RuntimeException("删除异常---->" + getClass().getSuperclass().getSimpleName(), e);
		}
	}

	/**
	 * 根据mac地址反向获取到对应的设备
	 * 
	 * @param model
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月21日 下午4:40:50
	 */
	public Wxdevice getDeviceByMac(Wxmac model) {
		// 获取门店信息
		Wxstore store = Wxstore.me.findFirst("SELECT * FROM ww_store WHERE shop_id = ?", model.get("shop_id"));
		// 设置apiconfig
		ApiConfigUtils.setApiConfig(model.getInt("wxuser_id"));
		// 微信接口查询设备所有信息
		List<Map<String, Object>> list = WifiApi.getAllDeviceInfo(model.getInt("shop_id"));
		// 根据mac地址比对后返回对应的本地设备
		for (Map<String, Object> map : list) {
			if (map.get("bssid").toString().equals(model.getStr("address"))) {
				Wxdevice device = Wxdevice.me.findFirst("SELECT * FROM ww_device WHERE ssid = ? AND shop_id = ?",
						map.get("ssid"), model.getInt("shop_id"));
				return device;
			}
		}
		return null;
	}
}
